package com.zxcl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxcl.domain.Notice;
import com.zxcl.dto.PageDTO;
import com.zxcl.dto.NoticeQueryDTO;
import com.zxcl.model.Result;
import com.zxcl.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述: [公告管理]
 * 作者: zxcl
 */
@RestController
@Api(tags = "公告管理")
@RequestMapping("shop/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;


    @GetMapping("page")
    @ApiOperation("分页查询公告")
    @PreAuthorize("hasAuthority('shop:notice:page')")
    public Result<Page<Notice>> loadNoticePage(PageDTO pageDTO, NoticeQueryDTO noticeQueryDTO) {
        Page<Notice> noticePage = noticeService.loadNoticePage(pageDTO, noticeQueryDTO);
        return Result.success(noticePage);
    }


    @PostMapping
    @ApiOperation("新增公告")
    @PreAuthorize("hasAuthority('shop:notice:save')")
    public Result<String> addNotice(@RequestBody Notice notice) {
        int i = noticeService.addNotice(notice);
        return Result.handle(i > 0);
    }

    /**
     * 查询商城首页的公告
     */

    @GetMapping("topNoticeList")
    @ApiOperation("查询商城首页的公告")
    public Result<List<Notice>> findMallNotices() {
        List<Notice> notices = noticeService.findMallNotices();
        return Result.success(notices);
    }
    /**
     * 查询商城首页的公告详情列表
     */
    @GetMapping("noticeList")
    @ApiOperation("查询商城首页的公告详情列表")
    public Result<List<Notice>> findMallNoticeList() {
        List<Notice> notices = noticeService.findMallNoticeList();
        return Result.success(notices);
    }

    /**
     * 根据ID查询一个公告信息
     */
    @GetMapping("info/{id}")
    @ApiOperation("根据ID查询一个公告信息")
    public Result<Notice> info(@PathVariable Long id) {
        Notice notice = noticeService.getById(id);
        return Result.success(notice);
    }


}
